/* 2-dim double vector.

   Copyright (C) 2001, 2002, 2003 Stefan Maierhofer.

   Written by Stefan Maierhofer <sm@cg.tuwien.ac.at>

   This file is part of Vecmath.

   Vecmath is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   Vecmath is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Vecmath; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */

using System;
using NUnit.Framework;

namespace Vecmath
{

    /// <summary>
    /// Vec2D.
    /// </summary>
    public struct Vec2D
    {
        public static Vec2D Identity = new Vec2D(0);
        public static Vec2D UnitX = new Vec2D(1,0);
        public static Vec2D UnitY = new Vec2D(0,1);

        public Vec2D(double xy)
        {
            _x = new double[2];
            _x[0] = xy; _x[1] = xy;
        }

        public Vec2D(Vec2D xy)
        {
            _x = new double[2];
            _x[0] = xy.x; _x[1] = xy.y;
        }

        public Vec2D(double x, double y)
        {
            _x = new double[2];
            _x[0] = x; _x[1] = y;
        }

        public Vec2D(ref double [] x)
        {
            _x = new double[2];
            _x[0] = x[0]; _x[1] = x[1];
        }

        public double this [int index]
        {
            get { return _x[index]; }
            set { _x[index] = value; }
        }

        public double x
        {
            get { return _x[0]; }
            set { _x[0] = value; }
        }

        public double y
        {
            get { return _x[1]; }
            set { _x[1] = value; }
        }

        public static implicit operator Vec2D(Vec2F v)
        {
            return new Vec2D(v.x, v.y);
        }

        public static implicit operator Vec2D(Pnt2D v)
        {
            return new Vec2D(v.x, v.y);
        }

        public static Vec2D operator+(Vec2D v, double f)
        {
            return new Vec2D(v[0] + f, v[1] + f);
        }

        public static Vec2D operator+(Vec2D v, Vec2D w)
        {
            return new Vec2D(v[0] + w[0], v[1] + w[1]);
        }

        public static Vec2D operator-(Vec2D v, double f)
        {
            return new Vec2D(v[0] - f, v[1] - f);
        }

        public static Vec2D operator-(Vec2D v, Vec2D w)
        {
            return new Vec2D(v[0] - w[0], v[1] - w[1]);
        }

        public static Vec2D operator-(Vec2D v)
        {
            return new Vec2D(-v[0], -v[1]);
        }

        public static Vec2D operator*(Vec2D v, double f)
        {
            return new Vec2D(v[0] * f, v[1] * f);
        }

        public static Vec2D operator*(Vec2D v, Vec2D w)
        {
            return new Vec2D(v[0] * w[0], v[1] * w[1]);
        }

        public static Vec2D operator/(Vec2D v, double f)
        {
            return new Vec2D(v[0] / f, v[1] / f);
        }

        public static double Dot(Vec2D u, Vec2D v)
        {
            return u[0] * v[0] + u[1] * v[1];
        }

        public double Dot(Vec2D v)
        {
            return this[0] * v[0] + this[1] * v[1];
        }

        public void Normalize()
        {
            this /= Length;
        }

        public Vec2D Normalized
        {
            get { return this / Length; }
        }

        public double SquaredLength
        {
            get { return _x[0] * _x[0] + _x[1] * _x[1]; }
        }

        public double Length
        {
            get { return (double)Math.Sqrt(SquaredLength); }
        }

        public int MinDim
        {
            get
            {
                return (_x[0] < _x[1]) ? 0 : 1;
            }
        }

        public int MaxDim
        {
            get
            {
                return (_x[0] > _x[1]) ? 0 : 1;
            }
        }

        public double Minimum
        {
            get
            {
                return (_x[0] < _x[1]) ? _x[0] : _x[1];
            }
        }

        public double Maximum
        {
            get
            {
                return (_x[0] > _x[1]) ? _x[0] : _x[1];
            }
        }

        public static bool operator<(Vec2D a, Vec2D b)
        {
            return (
                a._x[0] < b._x[0] &&
                a._x[1] < b._x[1]
                );
        }

        public static bool operator<=(Vec2D a, Vec2D b)
        {
            return (
                a._x[0] <= b._x[0] &&
                a._x[1] <= b._x[1]
                );
        }

        public static bool operator==(Vec2D a, Vec2D b)
        {
            return (
                a._x[0] == b._x[0] &&
                a._x[1] == b._x[1]
                );
        }

        public static bool operator!=(Vec2D a, Vec2D b)
        {
            return (
                a._x[0] != b._x[0] ||
                a._x[1] != b._x[1]
                );
        }

        public static bool operator>=(Vec2D a, Vec2D b)
        {
            return (
                a._x[0] >= b._x[0] &&
                a._x[1] >= b._x[1]
                );
        }

        public static bool operator>(Vec2D a, Vec2D b)
        {
            return (
                a._x[0] > b._x[0] &&
                a._x[1] > b._x[1]
                );
        }

        public static Vec2D Min(Vec2D a, Vec2D b)
        {
            return new Vec2D(
                Math.Min(a._x[0], b._x[0]),
                Math.Min(a._x[1], b._x[1])
                );
        }

        public static Vec2D Max(Vec2D a, Vec2D b)
        {
            return new Vec2D(
                Math.Max(a._x[0], b._x[0]),
                Math.Max(a._x[1], b._x[1])
                );
        }

        internal double [] _x;

        public override bool Equals(object obj)
        {
            Vec2D x = (Vec2D)obj;
            return (
                _x[0] == x._x[0] &&
                _x[1] == x._x[1]
                );
        }

        public override int GetHashCode()
        {
            return base.GetHashCode ();
        }

        public override string ToString()
        {
            return "Vec2D(" + _x[0] + ", " + _x[1] + ")";
        }

        static Vec2D()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture =
                System.Globalization.CultureInfo.InvariantCulture;
        }
    }
    

    [TestFixture]
    public class Vec2DTest
    {
        [Test]
        public void Initialization()
        {
            Vec2D v;

            v = Vec2D.Identity;
            Assert.IsTrue(
                v._x[0] == 0 && v._x[1] == 0
                );

            v = Vec2D.UnitX;
            Assert.IsTrue(
                v._x[0] == 1 && v._x[1] == 0
                );

            v = Vec2D.UnitY;
            Assert.IsTrue(
                v._x[0] == 0 && v._x[1] == 1
                );

            v = new Vec2D(3.14);
            Assert.IsTrue(
                v._x[0] == 3.14 && v._x[1] == 3.14
                );
            
            v = new Vec2D(0.1, 0.2);
            Assert.IsTrue(
                v._x[0] == 0.1 && v._x[1] == 0.2
                );

            double [] fa = { 0.1, 0.2 };
            v = new Vec2D(ref fa);
            Assert.IsTrue(
                v._x[0] == 0.1 && v._x[1] == 0.2
                );
        }

        [Test]
        public void Accessors()
        {
            Vec2D v = new Vec2D(0.1, 0.2);
            Assert.IsTrue(
                v._x[0] == v[0] && v._x[1] == v[1]
                );
            Assert.IsTrue(
                v._x[0] == v.x && v._x[1] == v.y
                );
        }

        [Test]
        public void ScalarOperators()
        {
            Vec2D v = new Vec2D(0.1, 0.2);

            v += 1.0;
            Assert.IsTrue(Vecmath.ApproxEquals(v, new Vec2D(1.1, 1.2)));

            v -= 1.0;
            Assert.IsTrue(Vecmath.ApproxEquals(v, new Vec2D(0.1, 0.2)));
            
            v *= 2.0;
            Assert.IsTrue(Vecmath.ApproxEquals(v, new Vec2D(0.2, 0.4)));

            v /= 2.0;
            Assert.IsTrue(Vecmath.ApproxEquals(v, new Vec2D(0.1, 0.2)));
        }

        [Test]
        public void VecOperators()
        {
            Vec2D v;

            v = new Vec2D(1,2) + new Vec2D(3,2);
            Assert.IsTrue(Vecmath.ApproxEquals(v, new Vec2D(4, 4)));

            v = new Vec2D(1,2) - new Vec2D(3,2);
            Assert.IsTrue(Vecmath.ApproxEquals(v, new Vec2D(-2, 0)));
        }

        [Test]
        public void Length()
        {
            Vec2D v = new Vec2D(0.1, 0.2);
            Assert.IsTrue(Vecmath.ApproxEquals(v.Length * v.Length, v.SquaredLength));
        }

        [Test]
        public void RelationalOperators()
        {
            Assert.IsTrue(new Vec2D(2,3) < new Vec2D(3,10));
            Assert.IsTrue(new Vec2D(2,3) <= new Vec2D(3,10));
            Assert.IsTrue(new Vec2D(2,3) != new Vec2D(3,10));
            Assert.IsFalse(new Vec2D(2,3) == new Vec2D(3,10));
            Assert.IsFalse(new Vec2D(2,3) >= new Vec2D(3,10));
            Assert.IsFalse(new Vec2D(2,3) > new Vec2D(3,10));
            
            Assert.IsFalse(new Vec2D(2,3) < new Vec2D(1,0));
            Assert.IsFalse(new Vec2D(2,3) <= new Vec2D(1,0));
            Assert.IsTrue(new Vec2D(2,3) != new Vec2D(1,0));
            Assert.IsFalse(new Vec2D(2,3) == new Vec2D(1,0));
            Assert.IsTrue(new Vec2D(2,3) >= new Vec2D(1,0));
            Assert.IsTrue(new Vec2D(2,3) > new Vec2D(1,0));

            Assert.IsFalse(new Vec2D(3,4) < new Vec2D(3,4));
            Assert.IsTrue(new Vec2D(3,4) <= new Vec2D(3,4));
            Assert.IsFalse(new Vec2D(3,4) != new Vec2D(3,4));
            Assert.IsTrue(new Vec2D(3,4) == new Vec2D(3,4));
            Assert.IsTrue(new Vec2D(3,4) >= new Vec2D(3,4));
            Assert.IsFalse(new Vec2D(3,4) > new Vec2D(3,4));

            Assert.IsFalse(new Vec2D(1,2) < new Vec2D(0,2));
            Assert.IsFalse(new Vec2D(1,2) <= new Vec2D(0,2));
            Assert.IsTrue(new Vec2D(1,2) != new Vec2D(0,2));
            Assert.IsFalse(new Vec2D(1,2) == new Vec2D(0,2));
            Assert.IsTrue(new Vec2D(1,2) >= new Vec2D(0,2));
            Assert.IsFalse(new Vec2D(1,2) > new Vec2D(0,2));

            Assert.IsFalse(new Vec2D(1,2) < new Vec2D(0,3));
            Assert.IsFalse(new Vec2D(1,2) <= new Vec2D(0,3));
            Assert.IsTrue(new Vec2D(1,2) != new Vec2D(0,3));
            Assert.IsFalse(new Vec2D(1,2) == new Vec2D(0,3));
            Assert.IsFalse(new Vec2D(1,2) >= new Vec2D(0,3));
            Assert.IsFalse(new Vec2D(1,2) > new Vec2D(0,3));
            
        }

        [Test]
        public void MinMax()
        {
            Vec2D v = new Vec2D(1,2);
            Assert.IsTrue(v.MinDim == 0);
            Assert.IsTrue(v.MaxDim == 1);
            Assert.IsTrue(v.Minimum == 1);
            Assert.IsTrue(v.Maximum == 2);
            Assert.IsTrue(Vec2D.Min(new Vec2D(1,2), new Vec2D(3,2)) == new Vec2D(1,2));
            Assert.IsTrue(Vec2D.Max(new Vec2D(1,2), new Vec2D(3,2)) == new Vec2D(3,2));
        }

        [Test]
        public void VecOps()
        {
            Vec2D u = new Vec2D(1,2);
            Vec2D v = new Vec2D(4,5);

            Assert.IsTrue(Vecmath.ApproxEquals(Vec2D.Dot(u,v), 14));
            Assert.IsTrue(Vecmath.ApproxEquals(u.Normalized.Length, 1));

            v.Normalize();
            Assert.IsTrue(Vecmath.ApproxEquals(v.Length, 1));
        }

        [Test]
        public void ToStringTest()
        {
            Vec2D v = new Vec2D(0.1, 0.2);
            Assert.AreEqual("Vec2D(0.1, 0.2)", v.ToString());
        }
    }

}
